package fr.mywiki.view.web.struts.form.entry;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.struts.action.ActionForm;

import fr.mywiki.business.entry.EntryLite;
import fr.mywiki.model.display.LinkToEntry;
import fr.mywiki.model.ejbAdapter.DataController;
import fr.mywiki.view.web.struts.action.entry.EntryPageAction;


/**
 * This formular displays a list of links to access specific entries.
 * 
 * @author tlombard
 */
public class EntryListForm extends ActionForm {
	private static final long serialVersionUID = 1L;

	/**
	 * A <code>collection</code> of <code>LinkToEntry</code> objects supposed to be directly
	 * accessed from the jsp.
	 */
	private Collection linksToEntries;
	
	/** The identifier of the last-clicked-on entry. */
	private Long entryId;
	
	/** The search string which drove to this formular */
	private String searchString;

	/**
	 * Initialises the formular with a list of entries given in parameters.
	 * 
	 * @param entries a <code>Collection</code> of <code>EntryLite</code> objects.
	 */
	public void init(Collection entries) throws RemoteException {
		linksToEntries = new ArrayList();

		HashMap map = null; // The map used by getLinkToEntry()

		Iterator itEntries = entries.iterator();
		while (itEntries.hasNext()) {
			Long id = (Long) itEntries.next();
			EntryLite entry = (EntryLite) DataController.getValue(id, EntryLite.TYPE_ENT);

			map = new HashMap();
			map.put("method","displayEntry");
			map.put(EntryPageAction.ENTRY_ID,entry.getId());
			
			// building the link to this entry
			linksToEntries.add(new LinkToEntry(
					entry.getId().toString(),
					(String) entry.get(EntryLite.FIELD_TITLE)));
		}
	}

	public Collection getLinksToEntries() {
		return linksToEntries;
	}

	public Long getEntryId() {
		return entryId;
	}

	public void setEntryId(Long entryId) {
		this.entryId = entryId;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}
}
